package com.luo.service;

import com.luo.mapper.DepartmentMapper;
import com.luo.pojo.Department;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    public Map<String,Object> selectDepartmentList(String departmentName,
                                                   String departmentCode,
                                                   int page,
                                                   int limit){
        int index = (page - 1) * limit;// 根据前端传递的页码和每页显示多少条，计算出当前页起始的数据下标值
        Map<String,Object> map = new HashMap<>();
        // 根据分页完成部门数据查询
        List<Department> departments = departmentMapper.selectDepartmentList(departmentName,departmentCode,index,limit);
        // 统计所有部门个数
        int count = departmentMapper.selectDepartmentCount(departmentName,departmentCode);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",departments);
        return map;
    }

    public Map<String,Object> insert(Department department){
        Map<String,Object> map = new HashMap<>();
        departmentMapper.insert(department);
        map.put("code",200);
        map.put("msg","新增部门成功");
        map.put("data",department);
        return map;
    }

    public Map<String,Object> update(Department department){
        Map<String,Object> map = new HashMap<>();
        departmentMapper.update(department);
        map.put("code",200);
        map.put("msg","更新部门成功");
        map.put("data",department);
        return map;
    }

    public Map<String,Object> delete(Integer id){
        Map<String,Object> map = new HashMap<>();
        departmentMapper.delete(id);
        map.put("code",200);
        map.put("msg","删除部门成功");
        map.put("id",id);
        return map;
    }

    public Map<String, Object> selectDepartmentNames() {
        Map<String, Object> map = new HashMap<>();
        List<Department> departments = departmentMapper.selectDepartmentNames();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", departments);
        return map;
    }
}
